package org.flower.dao.impl;

import org.flower.dao.ProvinceDao;
import org.flower.entity.Province;
import org.flower.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 省市区信息接口实现
 */
public class ProvinceDaoImpl extends BaseDao implements ProvinceDao {

    @Override
    public List<Province> getAllProByPid(int pid) {
        String sql = "select * from province where pid=?";
        List<Province> proList = new ArrayList<Province>();
        ResultSet rs = super.executeQuery(sql, pid);
        try {
            while (rs.next()){
                Province pro = new Province(rs.getInt("id"),rs.getString("name"),
                    rs.getInt("pId"),rs.getInt("level"));
                proList.add(pro);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(pid==0){
            for(Province pro :proList){
                if(pro.getId()==222){
                    proList.remove(pro);
                    break;
                }
            }
        }
        return proList;
    }
}
